<?php

namespace app\comm\service;
use app\comm\model\OrderCar;
use app\comm\model\OrderDetails;
use app\comm\model\Order;
use app\comm\model\Back;
use app\comm\model\OrderPay;

use ServS\DbShowcollo;
use think\Db;
use ServS\DbConf;
use app\comm\service\GoodsService;
use app\comm\service\UserService;
/**
 * 我的订单
 * 购物车
 * 订单详请操作
 */
class UserOrderService{


/********************************购物车*****************************/
	
	/**
	 * 增加购物车，service 在控制器中获取数据
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function addcar($array){
		$goodservice =new GoodsService();
		$goods = $goodservice->getgoodbyid($array['goods_id']);
		$data =[	'uid' => $array['uid'],
					'session_id'   => $array['uid'],
					'goods_id'     => $array['goods_id'],
					'goods_sn'     => $goods['good_number'],
					'goods_name'   =>$goods['name'],
					'market_price' =>$goods['price'],
					'goods_price'  =>$goods['price'],
					'member_goods_price' =>$goods['price'],
					'goods_num'    =>  $array['sum'],
					'spec_price'   =>'1',//属性价格
					'spec_name'    =>'',	//shuxin gmincheng 
					'bar_code'     =>$goods['good_number'],
				    'selected'     =>0,
				    'prom_type'    =>1,
				    'prom_id'      =>1,
				    'storeid'      => $array['storeid'],
				    'specid'       =>$array['spaceid']
				];
		$chaxun = $data ;unset($chaxun['goods_num']);
		$selec = OrderCar::where('1=1')->where($chaxun)->select();
		if($selec){
			$res = OrderCar::where('1=1')->where($chaxun)->setInc('goods_num',1);
			return 1;
		}
		$ordercar = new OrderCar;

		$res = DbShowcollo::add($ordercar,$data);
		return $res;
	}

	/**
	 * 购物车列表，通过uid获取每人的订单
	 */
	public function getordercar($uid,$pre=0,$limit=10 ){
		$goodsservice = new GoodsService;
		$order = OrderCar::where('uid',$uid)->limit($pre,$limit)->select();
		$arpush = [];
		foreach ($order as $key => $value) {
			$val = $value->toArray();
			$val['goods'] = $goodsservice-> getgoodbyid($val['goods_id']);
			array_push($arpush,$val);	
		}
		return $arpush;
	}

	/**
	 * 根据id删除购物车
	 */
	public function deletcarbyid($ids){
		$car = OrderCar::destroy($ids);
		return $car;
	}

	/**
	 * 用id获取购物车的信息
	 */
	public function getcatbyid($id){
		$order = OrderCar::get($id)->toArray();
		return $order;
	}


/********************************订单详情*****************************/
	
	/**
	 * 增加订单，线上增加
	 */
	public function addorder($array){
		/******正真的价格计算还没有开始******/
		$order  	   = new Order;
		$orderdetails  = new OrderDetails;
		$goodsservice  = new GoodsService;
		$array['order_type'] = 1;
		$array['order_sn']   = $goodsservice->contenorder($array['uid']);
		//dd($array);die;
		//如果是是用优惠券就在此处判断优惠券，重新定义价格
		//-------------------进入订单表-------------------------------------";
		$orderdata     	 =	 $this->orderdatadeal($array);
		$orderdataresl   =	 DbShowcollo::add($order,$orderdata);
		//-------------------订单详情表--------------------------------------"
		$array["order_id"]=$orderdataresl;
	
		$orderdetaildata =  $this->orderdatadetaile($array);
		$orderderesl     =  DbShowcollo::add($orderdetails,$orderdetaildata);
		//-------------------订单动态表---------------------------------------"

		//return $orderderesl ;
		//商品数量,减去库存
		//return $array;
		$sum = $array["sum"];
		
		if($array["space_id"]=="-1"){
			$stock = Db::table('ys_store_goods_stock')->where('goods_id', $array['goods_id'])->select()[0]["stock"];
			$stortlog=["goods_id"=>$array['goods_id'], "order_id"=>$orderderesl,"goods_spec_id"=>"-1",
						"store_id"=>$array["storeid"], "types"=>1,	"ruku_num"=>0,"tuihuo_num"=>0,
						"chuku_num"=>$array["sum"],    "sunhao_num" =>0,	"remark"=>"下单减少，$sum",
						"cur_stock"=>$stock,"addtime"=>time()];
			Db::table('ys_store_goods_log')->insert($stortlog);		
			Db::table('ys_store_goods_stock')->where('goods_id', $array['goods_id'])->setDec('stock',$array["sum"]);
		}else{
			$stock = Db::table('ys_store_goods_stock')->where('goods_id', $array['goods_id'])->where('goods_spec_id', $array['space_id'])->select()[0]["stock"];
			$stortlog=["goods_id"=>$array['goods_id'], "order_id"=>$orderderesl,"goods_spec_id"=>$array["space_id"],
						"store_id"=>$array["storeid"], "types"=>1,	"ruku_num"=>0,"tuihuo_num"=>0,
						"chuku_num"=>$array["sum"],    "sunhao_num" =>0,	"remark"=>"下单减少，$sum",
						"cur_stock"=>$stock,"addtime"=>time()];
			$stortlog["title"]	=Db::table('ys_goods_spec_detail')->find($array["space_id"])["title"];	
			Db::table('ys_store_goods_log')->insert($stortlog);	
			Db::table('ys_store_goods_stock')->where('goods_id', $array['goods_id'])->where('goods_spec_id', $array['space_id'])->setDec('stock',$array["sum"]);
		}	
		//----------------------库存 日志-----------------------------------//
		$array["order_id"]	=  $orderdataresl;
		$orderdetaaction =  $this->orderdataaction($array);
		Db::table("ys_order_action")->insert($orderdetaaction);
		if($orderdataresl && $orderderesl ){
			return $orderdataresl;
		}else{
			return null;
		}
	}

	/**
	 * 购物车生成订单
	 */
	public function addorderbycar($poststr,$uid,$aray){
		$orderdetails      = new OrderDetails;
		$order  	       = new Order;
		$goodsservice  	   = new GoodsService;
		$ordercar 		   = new OrderCar;
		$ordercaids  	   = explode(',', $poststr);
		$orderid  = $goodsservice->contenorder($uid);
		$price    = [];
		$ordertailall=[];
		foreach ($ordercaids as $key => $value) {
			$data  	=  $this->ordercardatadetaile($value,$orderid);//优惠券计算每个订单的价格
			$data['addtime']	=	time();
			array_push($ordertailall, $data);
			//ys_store_goods_log
			array_push($price, $data['goods_price']);
		}
		
		$pr 	=	'';$score =	'';
		//价格计算
		foreach ($ordertailall as $key => $value) {
			$pr += $value['total_price'];
			$score += $value['score'];
		}

		$array 	='';					//优惠券id
		$post['dress'] 	  = 	$aray['dress'];
		$post['pay_type'] = 	1;
		$array 	=['order_sn' =>$orderid,	'uid' => $uid,	'coupon_id' => 1,	'pay_type'=>$post['pay_type'],
				'carid'   =>$ordercaids[array_rand($ordercaids)],'dress'=>$post['dress'],
				'price'   =>$pr,'score'=>$data["score"]];

		//------------------------订单-----------------------------------
		$datacarorder      =    $this->ordercardatadeal($array);
		$orderdataresl     =	 DbShowcollo::add($order,$datacarorder);

		//--------------------详情---------------------------------------
		foreach ($ordertailall as $key => &$value) {
			$value["order_id"] 	= 	$orderdataresl;
		}
		$orderdetails->allowField(true)->saveAll($ordertailall);

		//-----------------------库存记录----------------------------------------------
		$array['store_id'] 		= 	$datacarorder['store_id'];
		$array['order_id'] 		=	$orderdataresl;
		$orderdetaactionbycar 	=  $this->orderdetaactionbycar($array);
		foreach ($ordercaids as $key => $value) {
			$data  	=  $this->ordercardatadetaile($value,$orderid);
			if($data["spec_id"]=="-1"){
				$sum 	=	 $data["goods_num"];
				$stock = Db::table('ys_store_goods_stock')->where('goods_id', $data['goods_id'])->select()[0]["stock"];
				$stortlog=["goods_id"=>$data['goods_id'], "order_id"=>$orderdataresl,"goods_spec_id"=>"-1",
							"store_id"=>$data["storeid"], "types"=>1,	"ruku_num"=>0,"tuihuo_num"=>0,
							"chuku_num"=>$sum,    "sunhao_num" =>0,	"remark"=>"购物车下单减少，$sum",
							"cur_stock"=>$stock,"addtime"=>time()];
				Db::table('ys_store_goods_log')->insert($stortlog);		
				Db::table('ys_store_goods_stock')->where('goods_id', $data['goods_id'])->setDec('stock',$sum);
			}else{
				$sum 	=	 $data["goods_num"];
				$stock = Db::table('ys_store_goods_stock')->where('goods_id', $data['goods_id'])->where('goods_spec_id', $data['spec_id'])->select()[0]["stock"];
				$stortlog=["goods_id"=>$data['goods_id'], "order_id"=>$orderdataresl,"goods_spec_id"=>$data["spec_id"],
							"store_id"=>$data["storeid"], "types"=>1,	"ruku_num"=>0,"tuihuo_num"=>0,
							"chuku_num"=>$sum,    "sunhao_num" =>0,	"remark"=>"购物车下单减少，$sum",
							"cur_stock"=>$stock,"addtime"=>time()];
				$stortlog["title"]	=Db::table('ys_goods_spec_detail')->find($data["spec_id"])["title"];	
				Db::table('ys_store_goods_log')->insert($stortlog);	
				Db::table('ys_store_goods_stock')->where('goods_id', $data['goods_id'])->where('goods_spec_id', $data['spec_id'])->setDec('stock',$sum);
			}	
		}

		//-------------------------------------------------------------------------
		Db::table("ys_order_action")->insert($orderdetaactionbycar);
		//删除购物车
		foreach (explode(',', $poststr) as $key => $value) {
			DbShowcollo::dele($ordercar,$value);
		}
		return $orderdataresl ;
	}

	/**
	 * 订单查询,根据uid查询
	 */
	public function getmyorder($uid,$status=-1,$page=0,$limit=10){
		if($status==-1){
			$list = Order::where('uid','=', $uid)->field('*')->limit($page,$limit)->select();	
		}else{
			$list = Order::where('uid','=', $uid)->where('order_status','=', $status)
						->field('*')->limit($page,$limit)->select();
		}		
		$push = [];
		foreach ($list as $key => $value) {
			$ordr= $value->toArray();
			$ordr['orderdetail'] = $this -> getordetailbyorderid($ordr['id']);
			//$ordr['detail'] 	 = htmlspecialchars_decode($ordr['detail']);
			array_push($push, $ordr);
		}
		return $push;
	}

	/**
	 * 获取订单详情，id，获取单个每个用户的订单
	 */
	public function getorderbyid($id){
		$aray  = [];
		$order = Db::table('ys_order')->alias('order')->where('order.id',$id)->select()[0];
		$list  = Db::table('ys_order_details')->where('order_id',$id)->select();				
			foreach ($list as $k=> &$v) {
				$v['id'] 		= $order['id'];
				$v['consignee'] = $order['consignee'];
			    $v['country'] 	= $order['country'];
			    $v['province']  = $order['province'];
			    $v['city'] 		= $order['city'];
			    $v['district']  = $order['district'];
			    $v['twon'] 		= $order['twon'];
			    $v['address'] 	= $order['address'];
			    $v['goods_price'] = $order['goods_price'];
			    $v['pay_name']  = $order['pay_name'];   
			}		
		return $list ;
	}

	/**
	 * 按照id获取订单详情 包括商品
	 */
	public function getordetailbyid($id){
		$orderdetail = OrderDetails::get($id);
		if(!$orderdetail)return null;
		return $orderdetail->toArray();
	}

	/**
	 * 按照orderid   获取该订单下的多有详情
	 */
	public function getordetailbyorderid($orid){
		$goodsservice  	   = new GoodsService;
		$orderdetail = OrderDetails::where('order_id',$orid)->select();
		$push = [];
		foreach ($orderdetail as $key => $value) {
			$val = $value->toArray();
			$val['good']=$goodsservice->getgoodbyid($val['goods_id']);
			$val['spec']=$goodsservice->getspacallbyid($val['spec_id']);
			array_push($push, $val);
		}
		return $push;
	}

	/**
	 * 退款时的订单详情表
	 */
	public function ordtailebyidtuikuan($id){
		$goodsservice  	   = new GoodsService;
		$ordr   = Db::table('ys_order_details')->where('id',$id)->find();
		$goods  = $goodsservice->getgoodbyid($ordr["goods_id"]);
		$ordr['good']=$goods;
		return $ordr;
	}

	/**
	 * 修改其中一个字段，订单的
	 * @param  [type] $id        [那条数据]
	 * @param  [type] $array     修改的数组
	 */
	public function gunbidiandanbyid($id,$array){
		$res = Order::where('id',$id)->update($array);
		return $res; 
	}

	/**
	 * 通过条件获取订单列表
	 */
	public function orderlist(array $array){
		$order = Db::table('ys_order') -> alias('order')
					->join('ys_orderdetail orderdetail','order.id = orderdetail.orderid')
					->where('order.id =$id')->select();
		return $order ;
	}

	/**
	 * 订单获取订单详情 住id 是 订单的
	 */
	public function orderbyid($id){
		$order = Db::table('ys_view_order_detail')->where("id",$id)->select();
		return $order;
	}

	/**
	 * 订单获取订单详列表
	 */ 
	public function orderbyiddetaile($id){
		$order = Db::table('ys_view_didorder')->where("id",$id)->select();
		return $order;
	}

/*****************************************************************************************/
	/**
	 *插入退货表
	 */
	public function saveorderback($data){
		$orderback = new OrderBack();
		$res = DbShowcollo::add($orderback,$data);
		return $res;
	}

	/**
	 * 修改退货表
	 */
	public function updatorback($id,$array){
		$orderback = new OrderBack();
		$result = DbShowcollo::refresh($orderback,'id',$id,$array);
		return $result;
	}

/******************************************内置方法*******************************************/
	
	/**
	 * 生成order方法，线上生成的方法1   0线下
	 */
	protected function orderdatadeal($data){

		$goodsservice   = new GoodsService;
		$address 		= new UserService;
		if($data['space_id']!='-1'){
			$spec  = $goodsservice->specbyid($data['space_id']);
			$score = $spec['score'];
			$price = $spec['price'];
		}else{
			 $goodsbyzondia = $goodsservice->getgoodbyid($data["goods_id"]);
			 $score = $goodsbyzondia['score'];
			 $price = $data['price'];
		}
		$dresscn = $address->getonedress($data['dress']);
		$data = [ "uid"=>$data['uid'],
				  'goods_id'		=>$data['goods_id'],
				  "order_sn"        =>$data['order_sn'],
				  "store_id"        =>$data['storeid'],
				  "order_type"      =>$data['order_type'],
				  "is_tuihuo"       =>0,
				  "order_status"    =>0,
				  "shipping_status" =>0,
				  "pay_status"      =>0,
				  "goods_num" 		=>$data['sum'],
				  "consignee"       =>$dresscn['consignee'],
				  "country"         =>$dresscn['country'],
				  "province"        =>$dresscn['province'],
				  "city" 		    =>$dresscn['city'],
				  "twon"            =>$dresscn['area'],
				  "address"         =>$dresscn['area'],
				  "zipcode"         =>$dresscn['zipcode'],
				  "mobile"          =>$dresscn['mobile'],
				  "email"           =>$dresscn['email'],
				  "shipping_code"   =>'',
				  'final_amount'	=>$price*$data['sum'],
				  "shipping_name"   =>'',
				  "pay_code"        =>0,
				  "pay_type"        =>'',
				  "pay_name"        =>'',
				  "invoice_title"   =>'',
				  "goods_price"     =>$data['price'],
				  "shipping_price"  =>'', //邮费
				  "coupon_id"       =>'', //优惠券id$data['coupon_id'],
				  "coupon_price"    =>'', //优惠价
				  "order_amount"    =>'', //应付金额
				  "total_amount"    =>'', // 订单总价
				  "shipping_time"   =>time(),
				  "confirm_time"    =>'',
				  "pay_time"        =>'',
				  "transaction_id"  =>'', //第三方平台交易流水号
				  "discount"        =>'',
				  "user_note"       =>'',
				  "admin_note"      =>'',
				  "is_deleted"      =>0,	 
				  "score"   		=>$score*$data['sum']
				];
		return $data ;
	}

	/**
	 * 生成订单car的功能
	 */
	protected function ordercardatadeal($data){
		$address 	   = new UserService;
		$dresscn 	   = $address->getonedress($data['dress']);
		$goodsservice  = new GoodsService;
		$car  		   = 	$this->getcatbyid($data['carid']);
		$data  = [ "uid"   		    =>$data['uid'],
				  "order_type"      =>1,
				  "final_amount"	=>$data['price'],
				  "store_id"        =>$car['storeid'],
				  "is_tuihuo"       =>0,
				  "order_status"    =>0,
				  "order_sn"		=>$data["order_sn"],
				  "shipping_status" =>0,
				  "pay_status"      =>0,
				  "consignee"       =>$dresscn['consignee'],
				  "country"         =>$dresscn['country'],
				  "province"        =>$dresscn['province'],
				  "city" 		    =>$dresscn['city'],
				  "twon"            =>$dresscn['area'],
				  "address"         =>$dresscn['area'],
				  "zipcode"         =>$dresscn['zipcode'],
				  "mobile"          =>$dresscn['mobile'],
				  "email"           =>$dresscn['email'],
				  "shipping_code"   =>'',
				  "shipping_name"   =>'',
				  "pay_code"        =>0,
				  "pay_type"        =>'',
				  "pay_name"        =>'',
				  "invoice_title"   =>'',
				  "goods_price"     =>$data['price'],
				  "shipping_price"  =>'', //邮费
				  "coupon_id"       =>'', //优惠券id$data['coupon_id'],
				  "coupon_price"    =>'', //优惠价
				  "order_amount"    =>'', //应付金额
				  "total_amount"    =>'', // 订单总价
				  "shipping_time"   =>time(),
				  "confirm_time"    =>'',
				  "pay_time"        =>'',
				  "transaction_id"  =>'', //第三方平台交易流水号
				  "discount"        =>'',
				  "user_note"       =>'',
				  "admin_note"      =>'',
				  "is_deleted"      =>0 ,
				  'score'           =>$data["score"]
				];
			return $data;
	}

	/**
	 * 订单详情数据
	 */
	protected function orderdatadetaile($data){
		$goodsservice = new GoodsService;
		$goods = $goodsservice->getgoodbyid($data['goods_id']);
		if($data['space_id']!='-1'){
			$resrt = $goodsservice->getspacallbyid($data['space_id']);
			$sediao = $resrt['pric']['title'];
			$spec  = $goodsservice->specbyid($data['space_id']);
			$score = $spec['score'];
			
		}else{
			$sediao = "普通商品";
			$score = $goods["score"];
		}
		$data = [
				  "uid"=>$data['uid'],
				  "order_id"         	 =>	$data['order_id'],    //支付订单id
				  "spec_id"				 =>	$data['space_id'],  //商品属性表id、
				  "spec_detail"			 => $sediao,
				  "order_code"     		 =>	"", //订单号
				  "goods_id"        	 =>	$data['goods_id'], //商品id
				  "goods_name"     	     =>	$goods['name'],//商品名称
				  "total_price"			 => $data['price']*$data['sum'],
				  "goods_sn"       		 =>	$goods['good_number'],//商品货号
				  "goods_num"       	 =>	$data['sum'],//购买数量
				  "market_price"     	 =>	$data['price'], //市场价
				  "goods_price"      	 =>	$data['price'],//本店价
				  "member_goods_price"   =>	$data['price'],//会员折扣价
				  "spec_price"			 => $data['price'],
				  "is_give_score"        =>	0,//是否已经发放积分
				  "give_score" 		     =>	$score,//购买商品赠送积分
				  "is_send_coupon"       =>	0,//是否已经发放优惠券
				  "send_coupon"          =>	0,//发放优惠券的id
				  "bar_code"             =>	$goods['good_number'],//条码
				  "is_comment"   		 =>	'0',//是否评价
				  "is_tuihuo"   		 =>	'0',//0未退货,1.yituiguo 
				  "tuihuo_sign"          =>	'',//1全部，2部分
				  "tuihuo_num"       	 =>	'',//退货数量
				  "th_deal_result"       =>	'',//退货处理结果，0失败，1通过
				  "th_deal_time"         =>	'', //退货处理时间
				  "status"    			 =>	'0',//订单商品状态,0未支付，1已支付，2商家已确认，3商家已发货，4买家已收货，5买家申请退货，6卖家已退款，7订单完成
				  "delivery_id"          =>	'',//发货单ID
				  "reason"  			 =>	'', 
				  "img"        			 =>	'',
				  "is_online"        	 =>	'1',
				  "storeid"       		 =>	$data['storeid']
				];
				return $data;
	}

	/**
	 *购物车生订单详情表值
	 */
	protected function ordercardatadetaile($data,$orderid){
		$goodsservice = new GoodsService;
		$car  = 	$this->getcatbyid($data);
		if($car['specid']!='-1'){
				$resrt 			= 	$goodsservice->getspacallbyid($car['specid']);
				$speciddetail 		= $resrt["pric"]["title"];
				$spec  				= $goodsservice->specbyid($car['specid']);
				$market_price 		= $spec['price'];
				$goods_price 		= $spec['price'];
				$member_goods_price = $spec['price'];
				$score  			= $spec['score'];
		}else{
			$market_price 		= $car['market_price'];
			$goods_price 		= $car['goods_price'];
			$member_goods_price = $car['member_goods_price'];
			$speciddetail 		= '';
			$goods = $goodsservice->getgoodbyid($car['goods_id']);
			$score              =$goods['score'];
		}
		$goods = $goodsservice ->getgoodbyid($car['goods_id']);	
		$datacar = [
					  "uid"					 =>$car['uid'],
					  "order_code"     		 =>$orderid, //订单号
					  "goods_id"        	 =>$goods['id'],
					  "goods_name"     	     =>$goods['name'],//商品名称
					  "goods_sn"       		 =>$goods['good_number'],//商品货号
					  "goods_num"       	 =>$car['goods_num'],//购买数量
					  "market_price"     	 =>$market_price, //市场价
					  "goods_price"      	 =>$goods_price,//本店价
					  "total_price"			 =>$goods_price*$car['goods_num'],
					  "member_goods_price"   =>$member_goods_price,//会员折扣价
					  "is_give_score"        =>0,//是否已经发放积分
					  "give_score" 		     =>$score,//购买商品赠送积分
					  "is_send_coupon"       =>0,//是否已经发放优惠券
					  "send_coupon"          =>0,//发放优惠券的id
					  "spec_id"         	 =>$car['specid'],//商品规格id
					  "spec_detail"			 =>$speciddetail,
					  "spec_price"			 =>$goods_price,
					  "bar_code"             =>$goods['good_number'],//条码
					  "is_comment"   		 =>'0',//是否评价
					  "is_tuihuo"   		 =>'0',//是否退货，0未退货，1已退货
					  "tuihuo_sign"         =>'',//1全部，2部分
					  "tuihuo_num"       	 =>'',//退货数量
					  "th_deal_result"       =>'',//退货处理结果，0失败，1通过
					  "th_deal_time"         =>'', //退货处理时间
					  "status"    			 =>'0',//订单商品状态,0未支付，1已支付，2商家已确认，3商家已发货，4买家已收货，5买家申请退货，6卖家已退款，7订单完成
					  "delivery_id"          =>'',//发货单ID
					  "reason"  			 =>'', 
					  "img"        			 =>'',
					  "is_online"        	 =>'1',
					  "storeid"       		 =>$car['storeid'],
					  "score" 				 =>$score*$car['goods_num']
				];
		return $datacar;
	}

	/**
	 * 单个表的订单动态，数据
	 */
	public function orderdataaction($aray){
		$data =["uid"			 =>$aray['uid'],	
				"order_id"		 =>$aray['order_id'],	
				"action_user"	 =>0,	
				"order_status"	 =>0,	
				"shipping_status"=>0,	
				"pay_status"	 =>0,	
				"status_desc"	 =>"直接下订单",	
				"storeid"		 =>$aray['storeid'],
				"addtime"		 => time()
			];
		return $data;
	}
	/*
	 * 购物车的订单动态，数据
	 */
	public function orderdetaactionbycar($aray){
		$data =["uid"			 =>$aray['uid'],	
				"order_id"		 =>$aray['order_id'],	
				"action_user"	 =>0,	
				"order_status"	 =>0,	
				"shipping_status"=>0,	
				"pay_status"	 =>0,	
				"status_desc"	 =>"购物车下单",	
				"storeid"		 =>$aray['store_id'],
				"addtime"		 => time()
			];
		return $data;
	}

	/**
	 * 获取支付名称
	 */
	public function getpayname($pay_code){
		$str ='';
		switch ($pay_code) {
			case 1:
				$str='支付宝';
				break;
			case 2:
				$str='微信';
				break;
			case 3:
				$str='余额';
				break;
			default:
				$str='未知支付';
				break;	
		}
		return $str;
	}

	/**
	 * 属性价格
	 */
	public function shuxinjiage($data){
		if(!empty($data)){
			$goodsservice = new GoodsService;
			$ar = explode('`',$data);
			foreach ($ar as $key => $value) {
				//$good = $goodsservice ->getpacename($value);
				//dd($value);
			}
		}else{
			return 1;
		}	
	}

/*****************************************************订单详情**********************/

	/**
	 * 订单详情
	 */
	public function orderdetaile($id){
		 $goodsservice = new GoodsService;
		 $order =  Order::get($id)->toArray();
		 $orderdetail =  OrderDetails::where("order_id", $order['id'])->select();
		 $orders =[];
		 foreach ($orderdetail as $key => &$value) {
		 	$ordersd  = $value->toArray();
		 	$ordersd['spec'] =$goodsservice->getspacallbyid($ordersd['spec_id']);
			$ordersd['good']  =  $goodsservice ->getgoodsallbyid($ordersd["goods_id"]);
		 	$ordersd['sumprice'] =$ordersd['goods_num']*$ordersd['market_price'];
		 	$orders[] = $ordersd ;
		 }
		 $order['detaile'] = $orders;
		 return $order;
	}

	/**
	 * 退款 插入 back
	 */
	public function ordertailback($array){
	  	$data= [
	  		"order_detail_id"=>$array['id'],"type"=>$array['type'],"num"=>$array['sum'],
	  		"amount"=>$array['amount'],"reason"=>$array['reason'],"status"=>0,"image"=>$array['image'],
	  		"goodsid"=>$array['goodsid'],'addtime'=>time()
	  	];
	  	Db::table("ys_order_back")->insert($data);
	  	$detail=["tuihuo_num"=>$array['sum'],'tuihuo_sign'=>$array['type'],"th_deal_result"=>0,"th_deal_remark"=>$array['reason'],
	  			 "tuihuo_amount"=>$array['amount'],"reason" =>$array['reason'],"img" =>$array['image'],"is_tuihuo"=>1,
	  			 "tuihuo_time" =>time()
	  			];
	  	Db::table('ys_order_details')->where('id', $array['id'])->update($detail);
	}

	/**
	 * 查询商品积分
	 */

	/**
	 * 订单回滚机制
	 */
	public function orderrollback(){
	  $time = 	DbConf::getconf("orderrollback")[0]["value"];
	  $list = Db::table("ys_order")->where("rollback",0)->where("pay_status",0)->select();
	  foreach ($list as $key => $value) {
	  	//dd( $value);
	  	if(time()+$time>$value["addtime"]){
	  		$ordeta = $this->	ordetail($value["id"]);
	  	  foreach ($ordeta as $key => $value1) {
	  	  		$this->detaliroobak($value1);
	  	  }
	  	}
	  	$reaut = Db::table("ys_order")->where("id", $value["id"])->update(["rollback"=>1]);
	  }
	  

	}
	//订单详情
	public function ordetail($orid){
		$list = Db::table("ys_order_details")->where("order_id",$orid)->select();
		return $list;
	}
	//回滚
	public function detaliroobak($value){
		if($value["spec_id"]=="-1"){
			Db::table("ys_store_goods_stock")->where("goods_id",$value["goods_id"])
						->setInc('stock', $value["goods_num"]);
		}else{
			Db::table("ys_store_goods_stock")->where("goods_id",$value["goods_id"])
						->where("goods_spec_id",$value["spec_id"])->setInc('stock', $value["goods_num"]);
		}
		
	}

	/**
	 * 评论订单
	 */
	public function commitorder($data){
		$ord=["img_comment"=>$data["image"],"order_comment"=>$data["comment"],
			 "level_comment"=>$data["level_comment"],"time_comment"=>time()
			 ];
		$reaut = Db::table("ys_order")->where("id", $data["id"])->update($ord);
		return $reaut;
	}


}

